package com.gexiaobao.pigeon.ui.fragment.mine.organization.detail

import android.graphics.Paint
import android.os.Bundle
import androidx.fragment.app.activityViewModels
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.BaseFragment
import com.gexiaobao.pigeon.app.ext.init
import com.gexiaobao.pigeon.databinding.FragmentPassDetailMineBinding
import com.gexiaobao.pigeon.ui.fragment.mine.organization.activity.ActivityAboutThisOrg
import com.gexiaobao.pigeon.ui.fragment.mine.organization.activity.ActivityAboutThisPass
import com.gexiaobao.pigeon.ui.fragment.mine.organization.activity.ActivityAboutThisSeason
import com.gexiaobao.pigeon.ui.fragment.mine.organization.activity.ActivityNumberOfPigeonDelivered
import com.gexiaobao.pigeon.viewmodel.OrgRaceViewModel
import com.gexiaobao.pigeon.viewmodel.SharedViewModel
import me.hgj.jetpackmvvm.ext.util.setOnclickNoRepeat
import me.hgj.jetpackmvvm.util.startActivity

/**
 * @Author      : hxw
 * @Date        : 2024/5/29 17:30
 * @Describe    : 我的成绩（我的）
 */
class FragmentPassDetailMine : BaseFragment<OrgRaceViewModel, FragmentPassDetailMineBinding>() {

    private var passId: Int = 0
    private var memberId: Int = 0
    private var mRaceId: Int = 0
    private var mOrgIcon = ""
    private var mOrgName = ""
    private var mOrgRaceName = ""

    private val sharedViewModel: SharedViewModel by activityViewModels()

    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.includeThisOrgData.tvAboutThisOrgPigeonNo.paint.flags = Paint.UNDERLINE_TEXT_FLAG

        memberId = arguments?.getInt("memberId", 0)!!
        passId = arguments?.getInt("passId", 0)!!
        mRaceId = arguments?.getInt("raceId", 0)!!
        mOrgIcon = requireArguments().getString("orgIcon").toString()
        mOrgName = requireArguments().getString("orgName").toString()
        mOrgRaceName = requireArguments().getString("raceName").toString()

        onRefresh()

        mDatabind.swipeRefreshLayout.init {
            mViewModel.getDataAll(memberId, passId)
        }

        sharedViewModel.data.observe(viewLifecycleOwner) {
            passId = it.id
            onRefresh()
        }
    }

    private fun onRefresh() {
        showLoading(getString(R.string.loading))
        mViewModel.getDataAll(memberId, passId)
    }

    override fun createObserver() {
        super.createObserver()
        /** 获取综合数据 */
        mViewModel.detailAllResult.observe(this) {
            if (mDatabind.swipeRefreshLayout.isRefreshing) {
                mDatabind.swipeRefreshLayout.isRefreshing = false
            }
            mDatabind.dataResult = it.memberPassResultData
            mDatabind.dataPass = it.memberPassData
            mDatabind.dataOrg = it.memberLoftData
            mDatabind.dataSeason = it.userSeasonData

            val signMoney = it.memberPassData.signMoney
            val winMoney = it.memberPassData.winMoney
            mDatabind.includeThisPassData.tvAboutPassSignMoney.text = if (signMoney == "0") "--" else signMoney
            mDatabind.includeThisPassData.tvAboutPassWinMoney.text = if (winMoney == "0") "--" else winMoney

            val totalPayMoney = it.memberLoftData.totalPayMoney
            val totalWinMoney = it.memberLoftData.totalWinMoney
            mDatabind.includeThisOrgData.tvAboutOrgTotalPayMoney.text = if (totalPayMoney == "0") "--" else totalPayMoney
            mDatabind.includeThisOrgData.tvAboutOrgTotalWinMoney.text = if (totalWinMoney == "0") "--" else totalWinMoney

            val outCome = it.userSeasonData.outcome
            val inCome = it.userSeasonData.income
            mDatabind.includeThisSeasonData.tvAboutSeasonOutcome.text = if (outCome == "0") "--" else outCome
            mDatabind.includeThisSeasonData.tvAboutSeasonIncome.text = if (inCome == "0") "--" else inCome
        }
    }

    override fun onBindViewClick() {
        super.onBindViewClick()
        setOnclickNoRepeat(
            mDatabind.includeThisOrgData.llAboutOrg,
            mDatabind.includeThisPassData.llAboutPass,
            mDatabind.includeThisSeasonData.llSeason,
            mDatabind.includeThisOrgData.llNumberOfPigeonDelivered
        ) {
            when (it) {
                mDatabind.includeThisOrgData.llNumberOfPigeonDelivered -> {
                    startActivity<ActivityNumberOfPigeonDelivered>("memberId" to memberId)
                }

                mDatabind.includeThisOrgData.llAboutOrg -> {//关于公棚
                    startActivity<ActivityAboutThisOrg>(
                        "raceId" to mRaceId,
                        "memberId" to memberId,
                        "orgName" to mOrgName,
                        "orgIcon" to mOrgIcon,
                        "orgRaceName" to mOrgRaceName
                    )
                }

                mDatabind.includeThisPassData.llAboutPass -> {//关于本关
                    startActivity<ActivityAboutThisPass>(
                        "passId" to passId,
                        "memberId" to memberId,
                        "orgName" to mOrgName,
                        "orgIcon" to mOrgIcon,
                        "orgRaceName" to mOrgRaceName
                    )
                }

                mDatabind.includeThisSeasonData.llSeason -> {//赛季档案
                    startActivity<ActivityAboutThisSeason>("passId" to passId)
                }
            }
        }
    }

    companion object {

        fun newInstance(memberId: Int, passId: Int, raceId: Int, orgName: String, raceName: String, orgIcon: String): FragmentPassDetailMine {
            val fragment = FragmentPassDetailMine()
            val bundle = Bundle()
            bundle.putInt("memberId", memberId)
            bundle.putInt("passId", passId)
            bundle.putInt("raceId", raceId)
            bundle.putString("orgName", orgName)
            bundle.putString("raceName", raceName)
            bundle.putString("orgIcon", orgIcon)
            fragment.arguments = bundle
            return fragment
        }
    }
}